// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   WaveFormatNonPCMByte.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package uoc.libwave;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Class for storing a less than 8-bit Non-PCM WAVE file format and, for reading
 * and writing its content in Data I/O Streams.
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-08-30
 */
public class WaveFormatNonPCMByte extends WaveFormatNonPCM<Byte> {

    /**
     * WAVE Data Chunk
     */
    ChunkDataNonPCMByte chunkData;

    /**
     * 
     * @since 2013-08-31
     */
    WaveFormatNonPCMByte() {
    }

    /**
     *
     * @param HeaderChunkID
     * @param HeaderChunkSize
     * @param HeaderRiffType
     * @param FormatChunkID
     * @param FormatChunkSize
     * @param wFormatTag
     * @param wChannels
     * @param dwSamplesPerSec
     * @param dwAvgBytesPerSec
     * @param wBlockAlign
     * @param wBitsPerSample
     * @param cbSize
     * @param FactChunkID
     * @param FactChunkSize
     * @param dwSampleLength
     * @param DataChunkID
     * @param DataChunkSize
     * @param dis
     *
     * @since 2013-08-31
     */
    public WaveFormatNonPCMByte(String HeaderChunkID, int HeaderChunkSize,
            String HeaderRiffType, String FormatChunkID, int FormatChunkSize,
            short wFormatTag, short wChannels, int dwSamplesPerSec,
            int dwAvgBytesPerSec, short wBlockAlign, short wBitsPerSample,
            short cbSize, String FactChunkID, int FactChunkSize,
            int dwSampleLength, String DataChunkID, int DataChunkSize,
            DataInputStream dis) throws IOException {
        super(HeaderChunkID, HeaderChunkSize, HeaderRiffType, FormatChunkID,
                FormatChunkSize, wFormatTag, wChannels, dwSamplesPerSec,
                dwAvgBytesPerSec, wBlockAlign, wBitsPerSample, cbSize,
                FactChunkID, FactChunkSize, dwSampleLength);
        chunkData = new ChunkDataNonPCMByte(DataChunkID, DataChunkSize,
                wBitsPerSample, wChannels, dis);
    }

    /**
     *
     * @param wFormatTag
     * @param sample_data
     * @param lenght
     * @param wChannel
     * @param dwSamplesPerSec
     * @since 2013-08-31
     */
    WaveFormatNonPCMByte(short wFormatTag, byte[][] sample_data, int lenght,
            short wChannel, int dwSamplesPerSec) {
        super(4 + 26 + 12 + 8 + (wChannel * lenght), wFormatTag, wChannel,
                dwSamplesPerSec, dwSamplesPerSec * wChannel, wChannel,
                (short) 8, wChannel * lenght);
        chunkData = new ChunkDataNonPCMByte(wFormatTag, sample_data, lenght,
                wChannel);
    }

    /**
     * 
     * @return
     * @since 2013-08-31
     */
    @Override
    public Byte getMaxSample() {
        return chunkData.getMaxSample();
    }

    /**
     *
     * @param wChannel
     * @param i
     * @return
     * @since 2013-08-31
     */
    @Override
    public Byte getSample(short wChannel, int i) {
        return chunkData.getSample(wChannel, i);
    }

    /**
     *
     * @param wChannel
     * @param i
     * @param value
     * @since 2013-08-31
     */
    @Override
    public void setSample(short wChannel, int i, Byte value) {
        chunkData.setSample(wChannel, i, value);
    }

    /**
     *
     * @param start
     * @param size
     * @param _Channel
     * @param lframe
     * @since 2013-08-31
     */
    @Override
    public void getFrame(int start, int size, short _Channel, List<Byte> lframe) {
        lframe.clear();
        for (int i = 0; i < size; i++) {
            lframe.add(getSample(_Channel, start + i));
        }
    }

    /**
     *
     * @param start
     * @param lframe
     * @param _Channel
     * @since 2013-08-31
     */
    @Override
    public void setFrame(int start, List<Byte> lframe, short _Channel) {
        for (int i = 0; i < lframe.size(); i++) {
            setSample(_Channel, start + i, lframe.get(i));
        }
    }

    /**
     *
     * @param strDstFilePath
     * @throws IOException
     * @since 2013-08-31
     */
    @Override
    public void saveToFile(String strDstFilePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(strDstFilePath);
        DataOutputStream dos = new DataOutputStream(fos);
        headerWaveFile.save(dos);
        chunkFormatNonPCM.save(dos);
        chunkFact.save(dos);
        chunkData.save(dos);
        dos.close();
        fos.close();
    }

    /**
     *
     * @return
     * @since 2013-08-31
     */
    @Override
    public int getNumSamples() {
        return chunkData.getNumSamples();
    }
}
